﻿using Architecture.Domain.Dto.RoleDto;
using Architecture.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Domain.Service.RoleServices
{

    public class RoleService : IRoleService
    {
        /// <summary>
        ///  用户信息角色服务
        /// </summary>
        protected readonly IUserInfoRoleRepository _userInfoRoleRepository;

        /// <summary>
        /// 用户信息仓储
        /// </summary>
        protected readonly IUserInfoRepository _userInfoRepository;

        /// <summary>
        /// 角色仓储
        /// </summary>
        protected readonly IRoleRepository _roleRepository;

        /// <summary>
        ///  构造函数
        /// </summary>
        /// <param name="userInfoRoleRepository">用户信息角色服务</param>
        /// <param name="userInfoRepository">用户信息仓储</param>
        /// <param name="roleRepository">角色仓储</param>
        public RoleService(IUserInfoRoleRepository userInfoRoleRepository, IUserInfoRepository userInfoRepository, IRoleRepository roleRepository)
        {
            _userInfoRoleRepository = userInfoRoleRepository;
            _userInfoRepository = userInfoRepository;
            _roleRepository = roleRepository;
        }

        /// <summary>
        /// 添加角色信息
        /// </summary>
        /// <param name="role">角色</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> AddRole(Role role)
        {
            role.CreatedBy = "";
            role.CreatedDate = DateTime.Now;
            return await _roleRepository.AddAsync(role);
        }


        /// <summary>
        /// 逻辑删除角色信息
        /// </summary>
        /// <param name="roleId">角色主键</param>
        /// <returns>返回受影响的行数</returns>
        public async Task<int> DeletedRole(int roleId)
        {
            var res = await _roleRepository.GetModelAsync(roleId);
            res.IsDeleted = true;
            res.DeletedBy = "";
            res.DeletedDate = DateTime.Now;
            return await _roleRepository.UpdateAsync(res);
        }

        /// <summary>
        /// 查询所有角色信息
        /// </summary>
        /// <returns>返回角色信息列表</returns>
        public async Task<PagingRoleDto<Role>> GetAllRole(int PageIndex, int PageSize, string? RoleName)
        {
            var paging = new PagingRoleDto<Role>();
            var list = await _roleRepository.GetAsync(x=>!x.IsDeleted);

            if (!string.IsNullOrEmpty(RoleName))
            {
                list = list.Where(x => x.RoleName.Contains(RoleName)).ToList();
            }

            paging.totalCount = list.Count();
            paging.pageCount = (int)Math.Ceiling(list.Count() * 1.0 / PageSize);
            paging.list = list.OrderByDescending(x => x.CreatedDate).Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
            return paging;
        }

        public async Task<List<Role>> GetAllRoles(Role role)
        {
            return await _roleRepository.GetAsync(x => !x.IsDeleted);
        }

        /// <summary>
        /// 反填角色--用户信息
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns>返回角色--用户信息列表</returns>
        public async Task<object> GetModel(int roleId)
        {
            var list = from a in await _roleRepository.GetAllAsync()
                       join b in await _userInfoRoleRepository.GetAllAsync()
                       on a.RoleId equals b.RoleId
                       join c in await _userInfoRepository.GetAllAsync()
                       on b.UserId equals c.UserId
                       where a.RoleId == roleId & !a.IsDeleted && !b.IsDeleted && !c.IsDeleted
                       select new
                       {
                           a.RoleId,
                           a.RoleName,
                           b.UserId,
                           c.UserName
                       };
            return list.ToList();
        }

        /// <summary>
        /// 反填角色信息
        /// </summary>
        /// <param name="roleId">用户ID</param>
        /// <returns>反填角色信息</returns>
        public async Task<Role> GetModelById(int roleId)
        {
            return await _roleRepository.GetModelAsync(roleId);
        }

        /// <summary>
        /// 条件查询角色信息
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <returns>返回角色信息</returns>
        public async Task<List<Role>> GetRole(string roleName)
        {
            return await _roleRepository.GetAsync(x => x.RoleName == roleName);
        }

        /// <summary>
        /// 修改角色信息
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public async Task<int> UpdateRole(Role role)
        {
            role.UpdatedBy = "";
            role.UpdatedDate = DateTime.Now;
            return await _roleRepository.UpdateAsync(role);
        }

        /// <summary>
        /// 查询角色ID和角色名称
        /// </summary>
        /// <returns>返回角色信息列表</returns>
        public async Task<object> GetRoleInfo()
        {
            var list = from a in await _roleRepository.GetAllAsync()
                       where !a.IsDeleted
                       select new
                       {
                           id=a.RoleId,
                           label=a.RoleName
                       };
            return list.ToList();
        }
    }
}
